package com.training.action;

import com.training.bean.MvcBean;
import com.training.parser.XMLParser;
import org.jdom.JDOMException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.util.Map;

/**
 * Created by Ywen on 2018/12/2.
 */
public class ActionListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        String mvnConfig = servletContext.getInitParameter("mvc-config");
        String realPath = servletContext.getRealPath("\\");
        try {
            Map<String, MvcBean> mvcBeanMap = XMLParser.load(realPath.concat(mvnConfig));
            servletContext.setAttribute("mvcConfig", mvcBeanMap);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("MVC配置加载完成...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
